package com.meitianhui.convenitenbuy.module.home.presenter;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.http.MainApiDataSource;
import com.meitianhui.convenitenbuy.main.model.bean.AppToken;
import com.meitianhui.convenitenbuy.main.model.bean.SignInfo;
import com.meitianhui.convenitenbuy.module.home.contract.HomeContract;
import com.meitianhui.convenitenbuy.module.home.http.HomeApiDataSource;
import com.meitianhui.convenitenbuy.module.home.model.bean.AreaCode;
import com.meitianhui.convenitenbuy.module.home.model.bean.BannerBean;
import com.meitianhui.convenitenbuy.module.home.model.bean.HomeGoodsInfo;
import com.meitianhui.convenitenbuy.module.home.model.bean.RecommendGoods;
import com.meitianhui.convenitenbuy.module.home.model.bean.ZeroBuyImage;
import com.meitianhui.convenitenbuy.module.home.ui.HomeFragment;

import java.util.HashMap;
import java.util.Map;

public class HomePresenter implements HomeContract.Presenter {

    private HomeFragment context;
    private final HomeContract.View mView;

    public HomePresenter(HomeContract.View view, HomeFragment context) {
        this.mView = view;
        this.context = context;
    }

    @Override
    public void getAd() {
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("category", "c_app_popup");
        String json = new Gson().toJson(hashMap);

        HomeApiDataSource.getInstance()
                .getBanner(InfoProvider.getAppToken(App.getInstance()),
                        "gdAppAdvert.app.gdAppAdvertFind", json)
                .subscribe(new ResponseObserver<BaseResponse<BannerBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<BannerBean> response) {
                        mView.getAdSuccess(response.data);
                    }
                });
    }

    @Override
    public void getAppToken() {
        MainApiDataSource.getInstance()
                .getAppToken(Constants.APP_ID, Constants.PRIVATE_KEY)
                .subscribe(new ResponseObserver<BaseResponse<AppToken>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<AppToken> response) {
                        mView.getAppTokenSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                    }
                });
    }

    @Override
    public void signInToday() {
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("consumer_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        String json = new Gson().toJson(hashMap);

        HomeApiDataSource.getInstance()
                .signInToday(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "consumer.consumer.consumerSign", json)
                .subscribe(new ResponseObserver<BaseResponse<SignInfo>>(context.getActivity(), true) {
                    @Override
                    public void onSuccess(BaseResponse<SignInfo> response) {
                        mView.signInTodaySuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.onDialogDismiss(code, msg);
                    }
                });
    }

    @Override
    public void getBanner() {
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("category", "c_app_home");
        String json = new Gson().toJson(hashMap);

        HomeApiDataSource.getInstance()
                .getBanner(InfoProvider.getAppToken(App.getInstance()),
                        "gdAppAdvert.app.gdAppAdvertFind", json)
                .subscribe(new ResponseObserver<BaseResponse<BannerBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<BannerBean> response) {
                        mView.getBannerSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.onDialogDismiss(code, msg);
                    }
                });
    }

    @Override
    public void getRecommendGoods() {
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("category", "c_app_home");
        String json = new Gson().toJson(hashMap);

        HomeApiDataSource.getInstance()
                .getRecommendGoods(InfoProvider.getAppToken(App.getInstance()),
                        "goods.fgGoodsActivityHomeForConsumerFind", json)
                .subscribe(new ResponseObserver<BaseResponse<RecommendGoods>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<RecommendGoods> response) {
                        mView.getRecommendGoodsSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.onDialogDismiss(code, msg);
                    }
                });
    }

    @Override
    public void getZeroBuyImage() {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("category", "c_free_goods");
        String params = new Gson().toJson(paramsMap);

        HomeApiDataSource.getInstance()
                .getZeroBuyImage(InfoProvider.getAppToken(App.getInstance()),
                        "goods.freeGoodsBannerImageFind", params)
                .subscribe(new ResponseObserver<BaseResponse<ZeroBuyImage>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<ZeroBuyImage> response) {
                        mView.getZeroBuyImageSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                    }
                });
    }

    /**
     * 获取数据信息
     */
    @Override
    public void getGoodsInfo(String pageIndex, String pageSize, String server,
                             String label, String areaId) {
        Map<String, String> maps = new HashMap<>();
        maps.put("page_no", pageIndex);
        maps.put("page_size", pageSize);
        String pageJson = new Gson().toJson(maps);
        Map<String, String> hashMap = new HashMap<>();
        if (!TextUtils.isEmpty(label)) {
            hashMap.put("label_promotion", label);
        }
        if (!TextUtils.isEmpty(areaId)) {
            hashMap.put("area_id", areaId);
        }
        String paramsJson = new Gson().toJson(hashMap);

        HomeApiDataSource.getInstance()
                .getEdinburgh(InfoProvider.getAppToken(App.getInstance()), server, pageJson, paramsJson)
                .subscribe(new ResponseObserver<BaseResponse<HomeGoodsInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<HomeGoodsInfo> response) {
                        mView.getGoodsInfoSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.getGoodsInfoFail(code, msg);
                    }
                });
    }

    @Override
    public void getAreaCode(String latitude, String longitude, String memberId) {
        Map<String, String> map = new HashMap<>();
        map.put("longitude", longitude + "");
        map.put("latitude", latitude + "");
        map.put("consumer_id", memberId);
        String paramsJson = new Gson().toJson(map);

        HomeApiDataSource.getInstance()
                .getAreaCode(InfoProvider.getAppToken(App.getInstance()),
                        "member.areaCodeFind", paramsJson)
                .subscribe(new ResponseObserver<BaseResponse<AreaCode>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<AreaCode> response) {
                        mView.getAreaCodeSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.onDialogDismiss(code, msg);
                    }
                });
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

}
